package org.example.utils;

import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class GetClassFields {

    /**
     * @date 2022/11/8
     * @description 获取对象的所有字段和值
     */
    public static Map<String, Object> getDomainFieldsAndValue(Object domain) {
        Map<String, Object> map = new HashMap<>();
        Class domainClass = domain.getClass();
        Field[] fields = domainClass.getDeclaredFields();
        try {
            for (Field field : fields) {
                String fieldString = field.toString().substring(field.toString().lastIndexOf(".") + 1);//获取列名
                field.setAccessible(true);//获取私有
                map.put(fieldString,field.get(domain));
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return Result.fileMap();
        }
        return map;
    }

    /**

     * @date 2022/11/8
     * @description 获取对象的所有字段和值
     * @parameter 对象Class
     * @returns 返回对象所有的字段和值
     */
    public static Map<String, Object> getDomainFieldsAndValue(Class domainClass, Object object) {
        Map<String, Object> map = new HashMap<>();
        Field[] fields = domainClass.getDeclaredFields();
        try {
            for (Field field : fields) {
                String fieldString = field.toString().substring(field.toString().lastIndexOf(".") + 1);
                field.setAccessible(true);
                map.put(fieldString,field.get(object));
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return Result.fileMap();
        }
        return map;
    }

    /**

     * @date 2022/11/8
     * @description
     */
    public static List<String> getDomainFields(Object domain) {
        List<String> fieldsList = new ArrayList<>();
        Class domainClass = domain.getClass();
        Field[] fields = domainClass.getDeclaredFields();
        try {
            for (Field field : fields) {
                String fieldString = field.toString().substring(field.toString().lastIndexOf(".") + 1);//获取列名
                field.setAccessible(true);//获取私有
                fieldsList.add(fieldString);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return fieldsList;
    }

    public static String convertCamelCaseToSnakeCase(String input) {
        StringBuilder result = new StringBuilder();
        if (input != null && !input.isEmpty()) {
            result.append(Character.toLowerCase(input.charAt(0)));
            for (int i = 1; i < input.length(); i++) {
                char currentChar = input.charAt(i);
                if (Character.isUpperCase(currentChar)) {
                    result.append("_");
                    result.append(Character.toLowerCase(currentChar));
                } else {
                    result.append(currentChar);
                }
            }
        }
        return result.toString();
    }

}
